//
//  PinYinForObjc.m
//  Search
//
//  Created by LYZ on 14-1-24.
//  Copyright (c) 2014年 LYZ. All rights reserved.
//

#import "PinYinForObjc.h"
#import "pinyin.h"
#import "ChineseString.h"

@implementation PinYinForObjc

+ (NSString*)chineseConvertToPinYin:(NSString*)chinese {
    NSString *sourceText = chinese;
    HanyuPinyinOutputFormat *outputFormat = [[HanyuPinyinOutputFormat alloc] init];
    [outputFormat setToneType:ToneTypeWithoutTone];
    [outputFormat setVCharType:VCharTypeWithV];
    [outputFormat setCaseType:CaseTypeLowercase];
    NSString *outputPinyin = [PinyinHelper toHanyuPinyinStringWithNSString:sourceText withHanyuPinyinOutputFormat:outputFormat withNSString:@""];
    return outputPinyin;
    
    
}

+ (NSString*)chineseConvertToPinYinHead:(NSString *)chinese {
    HanyuPinyinOutputFormat *outputFormat = [[HanyuPinyinOutputFormat alloc] init];
    [outputFormat setToneType:ToneTypeWithoutTone];
    [outputFormat setVCharType:VCharTypeWithV];
    [outputFormat setCaseType:CaseTypeLowercase];
    NSMutableString *outputPinyin = [[NSMutableString alloc] init];
    for (int i=0;i <chinese.length;i++) {
        NSString *mainPinyinStrOfChar = [PinyinHelper getFirstHanyuPinyinStringWithChar:[chinese characterAtIndex:i] withHanyuPinyinOutputFormat:outputFormat];
        if (nil!=mainPinyinStrOfChar) {
            [outputPinyin appendString:[mainPinyinStrOfChar substringToIndex:1]];
        } else {
            break;
        }
    }
    return outputPinyin;
}

+ (NSArray *)pinYinHeadBySort:(NSArray *)strs {
    NSMutableArray *pinyinHeader = [NSMutableArray array];
    HanyuPinyinOutputFormat *outputFormat = [[HanyuPinyinOutputFormat alloc] init];
    [outputFormat setToneType:ToneTypeWithoutTone];
    [outputFormat setVCharType:VCharTypeWithV];
    [outputFormat setCaseType:CaseTypeLowercase];
    
    for (NSString *str in strs) {
        if (str && ![[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""] && str.length > 0) {
            NSString *py = [PinyinHelper toHanyuPinyinStringWithNSString:str withHanyuPinyinOutputFormat:outputFormat withNSString:@""];
            NSString *head = [[py substringToIndex:1] uppercaseString];
            [pinyinHeader addObject:head];
        }
        
    }
    //首字母去重
    NSArray *sortPinYin = [pinyinHeader sortedArrayUsingSelector:@selector(compare:)];
    NSMutableArray *result = [NSMutableArray array];
    for (int i = 0; i < sortPinYin.count; i++) {
        if (i != (sortPinYin.count - 1) && [sortPinYin[i] isEqualToString:sortPinYin[i + 1]]) continue;
        [result addObject:sortPinYin[i]];
    }
    return result;
}

+ (NSArray *)pinYinHead:(NSArray *)strs {
    NSMutableArray *pinyinHeader = [NSMutableArray array];
    HanyuPinyinOutputFormat *outputFormat = [[HanyuPinyinOutputFormat alloc] init];
    [outputFormat setToneType:ToneTypeWithoutTone];
    [outputFormat setVCharType:VCharTypeWithV];
    [outputFormat setCaseType:CaseTypeLowercase];
    
    for (NSString *str in strs) {
        if (str && ![[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] isEqualToString:@""] && str.length > 0) {
            NSString *py = [PinyinHelper toHanyuPinyinStringWithNSString:str withHanyuPinyinOutputFormat:outputFormat withNSString:@""];
            NSString *head = [[py substringToIndex:1] uppercaseString];
            [pinyinHeader addObject:head];
        }
        
    }
    //首字母排序
    NSArray *sortPinYin = [pinyinHeader sortedArrayUsingSelector:@selector(compare:)];
    return sortPinYin;
}

+ (NSArray *)chineseSort:(NSArray *)chinese {
    //Step2:获取字符串中文字的拼音首字母并与字符串共同存放
    NSMutableArray *chineseStringsArray=[NSMutableArray array];
    for(int i=0;i< chinese.count; i++){
        ChineseString *chineseString=[[ChineseString alloc]init];
        
        chineseString.string=[NSString stringWithString:[chinese objectAtIndex:i]];
        
        if(chineseString.string==nil){
            chineseString.string=@"";
        }
        
        if(chineseString.string && ![chineseString.string isEqualToString:@""]){
            NSString *pinYinResult=[NSString string];
            for(int j=0;j<chineseString.string.length;j++){
                NSString *singlePinyinLetter=[[NSString stringWithFormat:@"%c",pinyinFirstLetter([chineseString.string characterAtIndex:j])]uppercaseString];
                if (pinYinResult && singlePinyinLetter) {
                    pinYinResult=[pinYinResult stringByAppendingString:singlePinyinLetter];
                }
            }
            chineseString.pinYin=pinYinResult;
        }else{
            chineseString.pinYin=@"";
        }
        [chineseStringsArray addObject:chineseString];
    }
    
    //Step2输出转换为拼音首字母后的NSString数组
//    for(int i=0;i<[chineseStringsArray count];i++){
//        ChineseString *chineseString=[chineseStringsArray objectAtIndex:i];
//        NSLog(@"原String:%@----拼音首字母String:%@",chineseString.string,chineseString.pinYin);
//    }
    
    //Step3:按照拼音首字母对这些Strings进行排序
    NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinYin" ascending:YES]];
    [chineseStringsArray sortUsingDescriptors:sortDescriptors];
    
    //Step3按照拼音首字母排序后的NSString数组
    NSMutableArray *resultSections = [NSMutableArray array];
    //获取首字母并排序
    NSArray *pinyinHeads = [PinYinForObjc pinYinHeadBySort:chinese];
    for (NSString *pinyinHead in pinyinHeads) {
        NSMutableArray *section = [NSMutableArray array];
        for(int i = 0;i < chineseStringsArray.count; i++){
            ChineseString *chineseString=[chineseStringsArray objectAtIndex:i];
            if (chineseString.pinYin && chineseString.pinYin.length) {
                NSString *firstPY = [chineseString.pinYin substringToIndex:1];
                if (firstPY && [pinyinHead isEqualToString:firstPY]) {
                    [section addObject:chineseString.string];
                }
            }
            
        }
        [resultSections addObject:section];
    }
    
    // Step4:如果有需要，再把排序好的内容从ChineseString类中提取出来
    NSMutableArray *result=[NSMutableArray array];
    for(int i=0;i<[chineseStringsArray count];i++){
        [result addObject:((ChineseString*)[chineseStringsArray objectAtIndex:i]).string];
    }
    
    
    return resultSections;
    
}

+ (NSArray *)pinYinHeads:(NSArray *)chinese {
    //Step2:获取字符串中文字的拼音首字母并与字符串共同存放
    NSMutableArray *chineseStringsArray=[NSMutableArray array];
    for(int i=0;i< chinese.count; i++){
//        ChineseString *chineseString=[[ChineseString alloc]init];
        NSString *pinyin = @"";
        NSString *chineseString = [NSString stringWithString:[chinese objectAtIndex:i]];
        
        if(chineseString==nil){
            chineseString=@"";
        }
        
        if(chineseString && ![chineseString isEqualToString:@""]){
            NSString *pinYinResult=[NSString string];
            for(int j=0;j<chineseString.length;j++){
                NSString *singlePinyinLetter = [[NSString stringWithFormat:@"%c", pinyinFirstLetter([chineseString characterAtIndex:j])] uppercaseString];
                if (pinYinResult && singlePinyinLetter) {
                    pinYinResult = [pinYinResult stringByAppendingString:singlePinyinLetter];
                }
            }
            pinyin = pinYinResult;
        }else{
            pinyin = @"";
        }
        [chineseStringsArray addObject:pinyin];
    }
    return chineseStringsArray;
}
@end
